<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div class="logo">
        <router-link to="/welcome">
          <img src="../assets/logo.png" alt />
          <span>电商后台管理系统</span>
        </router-link>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse?'64px':'200px'">
        <!-- 切换伸缩按钮 -->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#41b883"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级菜单： -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单的模板区 -->
            <template slot="title">
              <i class="iconfont" :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单： -->
            <el-menu-item
              :index="`/${subitem.path}`"
              v-for="subitem in item.children"
              :key="subitem.id"
              @click="saveNavState(subitem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subitem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconsObj: {
        "125": "icon-user",
        "103": "icon-tijikongjian",
        "101": "icon-shangpin",
        "102": "icon-danju",
        "145": "icon-baobiao"
      },
      isCollapse: false,
      // 被激活的二级导航地址
      activePath: ""
    };
  },
  created() {
    this.getMenuList();

    // 通过session设置二级导航高亮
    this.activePath = "/" + window.sessionStorage.getItem("activePath");

    // 通过路由参数来设置二级导航高亮
    // console.log(this.$route.path);
    //  :default-active="this.$route.path"
  },
  methods: {
    /*     getMenuList() {
      this.$http.get("/menus").then(res => {
        this.menu = res.data.data;
      });
    }, */
    async getMenuList() {
      const { data: res } = await this.$http.get("/menus");
      if (res.meta.status !== 200) return this.$message.error(res.mate.msg);
      this.menuList = res.data;
    },
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    // 点击按钮切换菜单折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    // 保存二级链接的激活状态
    saveNavState(saveNavState) {
      window.sessionStorage.setItem("activePath", saveNavState);
      this.activePath = "/" + saveNavState;
    }
  }
};
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  // padding: 0;
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.logo {
  a {
    display: block;
    display: flex;
    flex-flow: row nowrap;
    align-items: center;
    text-decoration: none;
    font-style: none;
  }
  display: flex;
  flex-flow: row nowrap;
  align-items: center;
  // padding-left: 30px;
  img {
    width: 40px;
  }
  span {
    color: #fff;
    font-size: 20px;
    margin-left: 15px;
  }
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background: #4a5064;
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  color: #fff;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>